﻿using UnityEngine;
using System.Collections;
using Assets.Scripts.Interfaces;

using Assets.Scripts;

using System.Collections.Generic;
using System.Linq;


public class Control : MonoBehaviour, IAction, IPlatform 
{

    public Vector2 startPosition;
    public Vector2 endPosition;
    public float slowDown = 50f;
    public static bool flag = false;   
    private ImageFactory factory;

    // Use this for initialization
    void Start()
    {
        factory = new ImageFactory();
    }

    // Update is called once per frame
    void Update()
    {
       // TouchFromPC();
        switch (Application.platform)
        {
            case RuntimePlatform.Android:
                TouchFromMobile(MobileType.Android);
                break;

            case RuntimePlatform.WindowsEditor:
                TouchFromPC();
                break;

            default:
                Debug.Log("Not supported platfotm!");
                break;
        }
    }

    public static GameObject curr;
    public void CheckTouch(Vector3 vector, Phrases phrase)
    {
        if (curr)
        {
            switch (phrase)
            {
                case Phrases.Began:
                    startPosition = vector;
                    flag = true;
                    break;
                case Phrases.Moved:
                    if (flag)
                        curr.rigidbody2D.velocity = (Vector2)Move(curr.transform, startPosition, vector, slowDown);                    
                    break;
                case Phrases.Ended:
                    flag = false;
                    endPosition = vector;
                    break;
            }
        }
    }
    public void TouchFromPC()
    {
        if (Input.GetMouseButtonDown(0))
            CheckTouch(Input.mousePosition, Phrases.Began);

        if (Input.GetMouseButton(0))
            CheckTouch(Input.mousePosition, Phrases.Moved);

        if (Input.GetMouseButtonUp(0))
            CheckTouch(Input.mousePosition, Phrases.Ended);
    }

    public void TouchFromMobile(MobileType mobileType)
    {
        switch (mobileType)
        {
            case MobileType.Android:
                for (int i = 0; i < Input.touchCount; i++)
                {
                    if (Input.GetTouch(i).phase == TouchPhase.Began)
                        CheckTouch(Input.GetTouch(i).position, Phrases.Began);

                    if (Input.GetTouch(i).phase == TouchPhase.Moved)
                        CheckTouch(Input.GetTouch(i).position, Phrases.Moved);

                    else if (Input.GetTouch(i).phase == TouchPhase.Ended)
                        CheckTouch(Input.GetTouch(i).position, Phrases.Ended);
                }
                break;
        }
    }

    public object Move(Transform transform, Vector2 startPosition, Vector2 touchPosition, float slowDown)
    {
        return new Vector2((transform.position.x + touchPosition.x - startPosition.x) / slowDown, 0);
    }

}
